//app.js
import {
  Token
} from 'utils/token.js';
import {
  Config
} from 'utils/config.js';
const {
  MemberModel
} = require('./utils/member-model.js')
const memberModel = new MemberModel()
App({
  onLaunch: function (options) {
    const navigationBtnObj = wx.getMenuButtonBoundingClientRect()
    wx.getSystemInfo({
      success: res => {
        let modelmes = res.model
        if (modelmes.search('iPhone X') != -1) {
          this.globalData.isIphoneX = true
        }
        this.globalData.navigationHeight = res.statusBarHeight + navigationBtnObj.height + (navigationBtnObj.top - res.statusBarHeight) * 2
        this.globalData.navigationTop = navigationBtnObj.top
        this.globalData.navigationLeft = res.windowWidth - navigationBtnObj.right
        this.globalData.capsuleWidth = navigationBtnObj.width
      }
    })
  },
  onShow(options) {
    const query = options.query || {}
    this.isWechatCome(query, 'sourcePartnerSign')
    this.isWechatCome(query, 'uniGuid')
    this.globalData.isShare = query.isShare
    this.globalData.shareMemberId = query.shareMemberId
    this.globalData.activityId = query.activityId
    this.getInfo(false).then(res => {
      this.globalData.isLogin = res.data.login
    })
  },
  isWechatCome(query, session) {
    if (query[session]) {
      wx.setStorageSync(session, query[session])
      this.globalData[session] = query[session]
    } else {
      const result = wx.getStorageSync(session)
      this.globalData[session] = result
    }
  },
  getInfo(refreshToken = true) {
    return new Promise((resolve, reject) => {
      wx.login({
        success: ({
          code
        }) => {
          this.login({
            appId: Config.appId,
            code,
            refreshToken,
            uniGuid: this.globalData.uniGuid,
            sourcePartnerSign: this.globalData.sourcePartnerSign
          }).then(data => {
            resolve({
              ...data,
              code
            })
          })
        }
      })
    })
  },
  login(userData) {
    const token = new Token()
    return new Promise((resolve, reject) => {
      token.getTokenFromServer(userData, function (data) {
        if (data.status === 200) {
          resolve(data)
        } else {
          wx.showToast({
            icon: 'none',
            title: data.error
          })
        }
      })
    })
  },
  /**
   * 埋点
   * mid 商户id
   * o 来源, 业务方自己定义, 1:砍价活动 2.捕鱼 3.手游
   * c 渠道, 1: 收银台 2:兑奖台 3:一体机 4:小程序 5:公众号
   * t 触发类型 1:访问 2:点击
   * e 触发事件与参数, 业务方自己定义, 格式为JSON
   */
  setTrackerLog(mid, o, c, t, e) {
    this.baseRestUrl = Config.restUrl + "/tracker/log";
    wx.request({
      url: this.baseRestUrl,
      data: {
        mid,
        o,
        c,
        t,
        e
      },
      method: 'get',
      success: function (res) {},
      fail: function (err) {}
    });
  },
  globalData: {
    userInfo: null,
    isIphoneX: false,
    sourcePartnerSign: '', // 跳转小程序携带参数
    uniGuid: '', // 跳转小程序携带参数
    isLogin: false, // 是否登录
    // isMerge: false // 是否合并

    // 分享统计
    isShare: '', // 是否从分享进来
    shareMemberId: '', // 分享人
    activityId: '', // 活动id

    // 自定义导航栏
    navigationHeight: 0, // 整个导航栏的高度
    navigationTop: 0, // 胶囊按钮与顶部的距离
    navigationLeft: 0, // 胶囊按钮与右侧的距离
    capsuleWidth: 0, // 胶囊按钮的宽度
  }
})